'use strict';

const Controller = require('egg').Controller;
const jwt=require("jsonwebtoken")

// const auth=(token)=>{
//   try{
//     jwt.verify(token,'zm')
//     return true
//   }catch{
//     return false
//   }
// }
class HomeController extends Controller {
  //登录
  async index() {
    const { ctx } = this;
    const {user,pass}=ctx.request.body
    const data=await this.app.mysql.get('user', { user,pass });
    if(data){
      const token=jwt.sign({user,pass},'zm',{expiresIn:"3d"})
      ctx.body = {
        code:200,
        token,
        data:data
      }
    }else{
      ctx.body={
        code:500,
        msg:"登录失败"
      }
    }
  }
  //注册
  async reg() {
    const { ctx } = this;
    const {user,pass}=ctx.request.body
    const data=await this.app.mysql.get('user', { user });
    if(data){
      ctx.body = {
        code:500,
        msg:"用户已存在"
      }
    }else{
      const res= await this.app.mysql.insert('user', { user,pass ,type:'访客',pan:'可用',jin:'启用',sou:'授权'});
      if(res){
        ctx.body={
          code:200,
          msg:"注册成功"
        }
      }else{
       ctx.boxy={
        code:500,
        msg:"注册失败"
      } 
      }
    }
  }
  //用户名
  async username() {
    const { ctx } = this;
    const {token}=ctx.request.body
    ctx.body={
      code:200,
      user:jwt.verify(token,'zm')
    }
  }
  //获取数据
  async list() {
    const { ctx } = this;
    const data=await this.app.mysql.select('lists');
    ctx.body={
      code:200,
      data
    }
  }
  //删除
  async del() {
    const { ctx } = this;
    const {id}=ctx.request.body
    const res=await this.app.mysql.delete('lists', {id});
    if(res){
      const data=await this.app.mysql.select('lists');
      ctx.body={
        code:200,
        data
      }
    }
  }
  //增加
  async add() {
    const { ctx } = this;
    const {id,name,xi,time}=ctx.request.body
    const res=await this.app.mysql.insert('lists', { id,name,xi,time })
    if(res){
      const data=await this.app.mysql.select('lists');
      ctx.body={
        code:200,
        data
      }
    }
  }
  //详情
  async detail() {
    const { ctx } = this;
    const {id}=ctx.request.body
    const data=await this.app.mysql.get('lists', { id });
      ctx.body={
        code:200,
        data
    }
  }

  //获取数据user
async user() {
  const { ctx } = this;
  const data=await this.app.mysql.select('user');
  ctx.body={
    code:200,
    data
  }
}


  //禁用
  async jin() {
    const { ctx } = this;
    const {id}=ctx.request.body
    const data=await this.app.mysql.get('user', { id:id });
    console.log(data)
    if(data.jin==='禁用'){
      await this.app.mysql.update("user",{id:id,pan:'已锁定',jin:"启用"})
      ctx.body={
      code:200,
    }
    }else{
      await this.app.mysql.update("user",{id:id,pan:'可用',jin:"禁用"})
      ctx.body={
        code:200
      }
    }
  }

  //授权
  async shou() {
    const { ctx } = this;
    const {id}=ctx.request.body
    const data=await this.app.mysql.get('user', { id:id });
    console.log(data)
    if(data.sou==='解除授权'){
      await this.app.mysql.update("user",{id:id,type:'访客',sou:"授权"})
      ctx.body={
      code:200,
    }
    }else{
      await this.app.mysql.update("user",{id:id,type:'管理员',sou:"解除授权"})
      ctx.body={
        code:200
      }
    }
  }
  //首焦推荐
  async fang() {
    const { ctx } = this;
    const {id}=ctx.request.body
    const data=await this.app.mysql.get('lists', { id:id });
    console.log(data)
    if(data.tui==='首焦推荐'){
      await this.app.mysql.update("lists",{id:id,tui:"取消推荐"})
      ctx.body={
      code:200,
    }
    }else if(data.tui==='取消推荐'){
      await this.app.mysql.update("lists",{id:id,tui:"首焦推荐"})
      ctx.body={
        code:200
      }
    }
  }
  //通过
  async tong() {
    const { ctx } = this;
    const {id}=ctx.request.body
    const data=await this.app.mysql.get('lists', { id:id });
    console.log(data)
    if(data.tong==='通过'){
      await this.app.mysql.update("lists",{id:id,tong:"通过"})
      ctx.body={
      code:200,
    }
    }else{
      await this.app.mysql.update("lists",{id:id,tong:"通过"})
      ctx.body={
      code:200,
    }
    }
  }
  //拒绝
  async ju() {
    const { ctx } = this;
    const {id}=ctx.request.body
    const data=await this.app.mysql.get('lists', { id:id });
    console.log(data)
    if(data.tong==='未通过'){
      await this.app.mysql.update("lists",{id:id,tong:"未通过"})
      ctx.body={
      code:200,
    }
    }else{
      await this.app.mysql.update("lists",{id:id,tong:"未通过"})
      ctx.body={
      code:200,
    }
    }
  }
  //回复
  async hui() {
    const { ctx } = this;
    const {id,name}=ctx.request.body
    const data=await this.app.mysql.get('lists', { id:id });
    console.log(data)
    if(data.tong==='未通过'){
      await this.app.mysql.update("lists",{id:id,tong:"未通过"})
      ctx.body={
      code:200,
    }
    }else{
      await this.app.mysql.update("lists",{id:id,tong:"未通过"})
      ctx.body={
      code:200,
    }
    }
  }
  //锁定
  async ding(){
    const {ctx}=this
    const {token}=ctx.request.body
    const user=jwt.verify(token,'zm')
    console.log(user)
    const data=await this.app.mysql.get('user',{user:user.user});
    console.log(data)
    if(data.pan==='可用'){
      ctx.body={
        code:200
      }
    }else{
      ctx.body={
        code:503
      }
    }
  }
  //用户管理
  async guan(){
    const {ctx}=this;
    const {token}=ctx.request.body;
    const usert=jwt.verify(token,'zm')
    console.log(usert)
    const data=await this.app.mysql.select('user');
    console.log(data)
    const data1=await this.app.mysql.get('user',{user:usert.user});
    console.log(data1)
    if(data1.type==='管理员'){
      ctx.body={
        code:200,
        data
      }
    }else{
      ctx.body={
        code:503
      }
    }
  }

   //页面管理数据
   async li() {
    const { ctx } = this;
    const data=await this.app.mysql.select('guan');
    ctx.body={
      code:200,
      data
    }
  }
  //删除页面管理数据
  async delli() {
    const { ctx } = this;
    const {id}=ctx.request.body
    const res=await this.app.mysql.delete('guan', {id});
    if(res){
      const data=await this.app.mysql.select('guan');
      ctx.body={
        code:200,
        data
      }
    }
  }

    //草稿
    async cao() {
      const { ctx } = this;
      const {id}=ctx.request.body
      const data=await this.app.mysql.get('guan', { id:id });
      console.log(data)
      if(data.xia==='下线'){
        await this.app.mysql.update("guan",{id:id,zhuang:"草稿",xia:"发布"})
        ctx.body={
        code:200,
      }
      }else{
        await this.app.mysql.update("guan",{id:id,zhuang:"已发布",xia:'下线'})
        ctx.body={
          code:200
        }
      }
    }


  //数据小册数据
  async ce() {
    const { ctx } = this;
    const data=await this.app.mysql.select('ce');
    ctx.body={
      code:200,
      data
    }
  }

}


module.exports = HomeController;
